package com.steven.ourcontest.forum.viewmodel

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.steven.ourcontest.base.BaseViewModel
import com.steven.ourcontest.forum.api.ForumApi
import com.steven.ourcontest.net.EmptyResponse
import com.steven.ourcontest.utils.plusAssign
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.RequestBody

class AddForumViewModel : BaseViewModel() {
    private val _addResponse = MutableLiveData<EmptyResponse?>()
    val addResponse: LiveData<EmptyResponse?>
        get() = _addResponse

    fun addForum(
        body: RequestBody,
        onSuccess: (() -> Unit),
        onError: ((throwable: Throwable) -> Unit)
    ) {
        disposed += ForumApi.instance.addForum(body)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                _addResponse.value = it.result
                onSuccess.invoke()
            }, {
                it.printStackTrace()
                onError.invoke(it)
            })
    }

}